#region File Description
//-----------------------------------------------------------------------------
// Game1.cs
//
// XNAHero.com
// Copyright (C) XNAHero.com. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace rendertexture3dxna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;

        Matrix View, Projection;
        TexturedQuad sprite;
        Texture2D texture;
        BasicEffect basicEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 512;
            graphics.PreferredBackBufferHeight = 512;
        }

        protected override void Initialize()
        {

            View = Matrix.CreateLookAt(new Vector3(0, 0, 500), Vector3.Zero,
                Vector3.Up);
            
            Projection = Matrix.CreateOrthographicOffCenter(-256, 256, -256, 256, 1, 1000);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>("SonicRoom");

            sprite = new TexturedQuad(new Vector3(0, 0, 0), Vector3.Backward, Vector3.Up, new Rectangle(0, 0, texture.Width, texture.Height), new Vector2(texture.Width, texture.Height), new Vector2(texture.Width, texture.Height));

            basicEffect = new BasicEffect(GraphicsDevice);
            //basicEffect.EnableDefaultLighting();

            View = Matrix.CreateLookAt(new Vector3(0, 0, 500), Vector3.Zero,
                Vector3.Up);

            Projection = Matrix.CreateOrthographicOffCenter(-256, 256, -256, 256, 1, 1000);

            basicEffect.World = Matrix.Identity;
            basicEffect.View = View;
            basicEffect.Projection = Projection;
            basicEffect.TextureEnabled = true;
            basicEffect.Texture = texture;
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

#if WINDOWS
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
#endif

            // TODO: Add your update logic here
            float rotation = MathHelper.WrapAngle((float)gameTime.TotalGameTime.TotalMilliseconds / 600);
            if (rotation < -MathHelper.PiOver2 || rotation > MathHelper.PiOver2)
                rotation = MathHelper.Pi + rotation;


            basicEffect.World = Matrix.CreateRotationY(rotation);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                GraphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    sprite.Vertices, 0, 4,
                    sprite.Indexes, 0, 2);
            }

            base.Draw(gameTime);
        }
    }
}
